#ifndef MACRORULE_H
#define MACRORULE_H

#include <QList>
#include "rule.h"
#include "core/translator.h"

// TODO: replace js wit ES, when ES can do simple math (2+2*2)
// also, creating QJSEngine is very costly
class QJSEngine;
extern QJSEngine js;

struct MacroVariable
  {
  MacroVariable(QString n, QString minV, QString maxV) :
    name(n), minValue(minV), maxValue(maxV) {}
  QString name;
  QString minValue;
  QString maxValue;
  };

class NCA_EXPORT MacroRule : public Rule
  {  
  friend class MacroRuleTest;
public:
  enum MacroOperation
    {
    MO_SUM,
    MO_PRODUCT
    };

  MacroRule(const Expression &pattern, const Expression &replacement,
            const QString& macroReplacement, MacroOperation op,
            QList<MacroVariable> vars, Translator &translator);
  MacroRule(const Rule &rule,
            const QString& macroReplacement, MacroOperation op,
            QList<MacroVariable> vars, Translator &translator);

  bool apply(Expression & exp, Position pos) const;
  MacroRule& operator=(const MacroRule &rhs);
  QList<Rule> generateRules(Expression const &exp);

  // TODO:
  // we can't actually apply a macro pattern, it should be disabled somehow

private:
  QList<int> getNextCombination(QList< QPair<int, int> > vars, QList<int> prevValues);

  QString _macroReplacement;
  MacroOperation _operation;
  QList<MacroVariable> _variables;

  Translator &_translator;
  };


#endif // MACRORULE_H
